import {getPoint, parseTime} from "../../utils/util";

/**
 * 增加需求，如果是心音、呼吸音、肠鸣音 显示选项、其他听诊不显示
 */
Component({
    properties: {
        options:{
            type:Array
        },
        positionName:{
            type:String
        },
        optionList:{
            type:Array
        },
        name:{
            type:String
        },
        radio:{
            type:Number
        },
        resultFlag:{
            type:Boolean
        },//是否查看结果
        interpretation:{
            type:Boolean
        },
        rightAnswers:{
            type:String
        },
        rightAnswersText:{
            type:String
        }
    },
    data: {
        show:true,
        active:0,
        timeSumLen:'00:00',
        timeNow:'00:00',
        innerAudioContext:null,
        isStart:false,
        optionIndex:-1,
        optionObj:{},
        modelImageList:[],
        loading:false,
        selectImg:'',
        scrollId:''
    },
    lifetimes: {
        // 组件的生命周期函数，用于声明组件的生命周期
        attached() {
            //positionName判断要显示那几张图片
            //通过name判断要显示到图片
            // switch (this.data.name){
            //     case '颈动脉听诊：左':
            //         this.data.modelImageList = ['f-1','f-2','f-3','f-4']
            //         break
            //     case '颈动脉听诊：右':
            //         this.data.modelImageList = ['f-1','f-2','f-3','f-4']
            //         break
            //     case '甲状腺区听诊':
            //         this.data.modelImageList = ['f-1','f-2','f-3','f-4']
            //         break
            //     case '呼吸音':
            //         this.data.modelImageList = ['f-1','f-2','f-3','f-4']
            //         break
            //     case '心音':
            //         this.data.modelImageList = ['x-1']
            //         break
            //     case '肠鸣音':
            //         this.data.modelImageList = ['c-1']
            //         break
            //     case '腹部血管杂音':
            //         this.data.modelImageList = ['c-1']
            //         break
            // }
            this.data.optionList.forEach(point=>{
                const obj = getPoint(point.name)
                point.x = obj ? obj.x :''
                point.y = obj ? obj.y :''
                point.image = obj ? obj.img :''
            })
            let imgList = []
            this.data.optionList.forEach(item=>{
                if(!imgList.includes(item.image)){
                    imgList.push(item.image)
                }
            })
            this.setData({
                optionList :this.data.optionList,
                modelImageList:imgList
            })
        },
        detached() {
            // 在组件实例被从页面节点树移除时执行
            if(this.data.innerAudioContext != null){
                this.data.innerAudioContext.stop()
                this.data.innerAudioContext.destroy()
            }
        },
    },
    methods: {
        /**
         * 播放音乐
         */
        play(){
            if(this.data.innerAudioContext == null){
                wx.showToast({
                    title: '请选择听诊点',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            //修改状态
            const optionObj = this.data.optionObj
            optionObj.status = true
            const optionList = this.data.optionList
            optionList[this.data.optionIndex].status = true
            this.setData({
                isStart:true,
                optionObj,
                optionList
            })
            this.data.innerAudioContext.play()
        },
        /**
         * 暂停播放
         */
        pause(){
            this.data.innerAudioContext.pause()
            this.setData({
                isStart:false
            })
        },
        /**
         * 点击音原菜单
         */
        optionItemClick(e){
            const item = e.currentTarget.dataset.obj
            const index = e.currentTarget.dataset.index
            this.setData({
                selectImg:item.image,
                scrollId:item.image,
                optionObj:item,
                optionIndex:index,
                timeNow:'00:00'
            })
            this.initAudio()
            this.play()
        },
        /**
         * 初始化播放器
         */
        initAudio(){
            //关闭所有播放
            //销毁
            if(this.data.innerAudioContext != null){
                this.data.innerAudioContext.stop()
                this.data.innerAudioContext.destroy()
            }
            this.setData({
                loading:true
            })
            wx.showLoading({title: '音源加载中', icon: 'loading', duration: 10000});
            const innerAudioContext = wx.createInnerAudioContext()
            innerAudioContext.autoplay = false
            innerAudioContext.loop = true
            innerAudioContext.src = 'https://zhongyi.tellyescloud.com/resource/'+this.data.optionObj.url
            innerAudioContext.onPlay(() => {
                console.log('开始播放')
            })
            innerAudioContext.onError((res) => {
                console.log(res.errMsg)
                console.log(res.errCode)
            })
            innerAudioContext.onCanplay(()=> {
                // 必须。可以当做是初始化时长
                innerAudioContext.duration;
                // 必须。不然也获取不到时长
                setTimeout(() => {
                    wx.hideLoading()
                    this.setData({
                        timeSumLen:parseTime(innerAudioContext.duration * 1000,'{i}:{s}'),
                        loading:false
                    })
                }, 200)
            })

            innerAudioContext.onTimeUpdate(()=>{
                console.log(innerAudioContext.currentTime)
                const time = innerAudioContext.currentTime.toString()
                const m = time.split('.')[0]
                const timeNow =parseTime(Number(m) * 1000,'{i}:{s}')
                this.setData({
                    timeNow:timeNow,
                })
                // if(this.data.timeSumLen == this.data.timeNow){
                //     this.setData({
                //         isStart:false,
                //     })
                // }
            })
            this.setData({
                innerAudioContext:innerAudioContext,
                isStart:false
            })
        },
        onTabChange(e){
            const index = e.target.dataset.index
            this.setData({
                active:index
            })
        },
        onChange(event) {
            if(this.data.resultFlag) return
            this.setData({
                radio: event.detail,
            });
        },
        /**
         *  确定
         */
        enter(){
            //关闭所有播放
            if(this.data.innerAudioContext != null){
                this.data.innerAudioContext.stop()
                this.data.innerAudioContext.destroy()
            }
            debugger
            //判断 如果是强制解读，必须进行作答
            if(this.data.interpretation && (this.data.radio == -1 || this.data.radio.toString() == '')){
                const name = this.data.name
                if(name ==='心音'|| name ==='呼吸音' || name ==='肠鸣音'){
                    this.setData({
                        active:1
                    })
                    wx.showToast({
                        title: '请选择检查结果！',
                        icon: 'none',
                        duration: 2000
                    })
                    return
                }
            }
            this.triggerEvent('onEnter',{radio:this.data.radio,optionList:this.data.optionList})
        },
        onClose(){
            //关闭所有播放
            if(this.data.innerAudioContext != null){
                this.data.innerAudioContext.stop()
                this.data.innerAudioContext.destroy()
            }
            this.triggerEvent('onClose')
        },
    },
});
